/*************************************************************************
 * Copyright 2008 David Almilli
 *  
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 *  
 *     http://www.apache.org/licenses/LICENSE-2.0 
 *      
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 **************************************************************************/
package com.tivo.hme.samples.bananas;

import com.tivo.hme.bananas.BApplicationPlus;
import com.tivo.hme.bananas.BEvent;
import com.tivo.hme.bananas.BShuttleBarPlus;
import com.tivo.hme.bananas.BTextPlus;
import com.tivo.hme.bananas.layout.Layout;
import com.tivo.hme.bananas.layout.LayoutManager;

public class ShuttleBarScreen extends BananasSampleScreen {
	private static final String[] MODES = { 
		BShuttleBarPlus.MODE_REVERSE3,
		BShuttleBarPlus.MODE_REVERSE2,
		BShuttleBarPlus.MODE_REVERSE1,
		BShuttleBarPlus.MODE_PLAY,
		BShuttleBarPlus.MODE_FORWARD1,
		BShuttleBarPlus.MODE_FORWARD2,
		BShuttleBarPlus.MODE_FORWARD3
	};
	private BShuttleBarPlus shuttleBar;

	public ShuttleBarScreen(BApplicationPlus app) {
		super(app);

        LayoutManager lm = new LayoutManager(getNormal());
        Layout safeTitle = lm.safeTitle(this);
        Layout layout = safeTitle;

        Layout contentLayout = lm.relativeY(layout);
        layout = lm.valign(layout, A_BOTTOM);
        layout = lm.stretchWidth(layout);

		shuttleBar = new BShuttleBarPlus(this, layout);
    	shuttleBar.setMode(BShuttleBarPlus.MODE_STOP);
		shuttleBar.setTickSpacing(600);

        layout = lm.valign(contentLayout, A_TOP+50);
        layout = lm.stretchWidth(layout);
        layout = lm.height(layout, 50);
		
		BTextPlus text = new BTextPlus(this, layout);
		text.setValue("Use up and down arrows to modify playbar value");
	}
    
    /**
     * Since there is only one button, we will treat select like a left 
     * action and pop the screen.
     */
    public boolean handleKeyPress(int code, long rawcode) 
    {
        switch (code) {
        case KEY_SELECT:
        case KEY_LEFT:
            postEvent(new BEvent.Action(this, "pop"));
            getBApp().play("select.snd");
            return true;
        case KEY_DOWN:
        	shuttleBar.setValue(shuttleBar.getValue()-600);
    		shuttleBar.setTickSpacing(1200);
            getBApp().play("select.snd");
        	return true;
        case KEY_UP:
        	shuttleBar.setValue(shuttleBar.getValue()+600);
    		shuttleBar.setTickSpacing(600);
            getBApp().play("select.snd");
        	return true;
        case KEY_PLAY:
        	shuttleBar.setMode(BShuttleBarPlus.MODE_PLAY);
            getBApp().play("select.snd");
        	return true;
        case KEY_SLOW:
            shuttleBar.setMode(BShuttleBarPlus.MODE_SLOW);
            getBApp().play("select.snd");
            return true;
        case KEY_PAUSE:
        	shuttleBar.setMode(BShuttleBarPlus.MODE_PAUSE);
            getBApp().play("select.snd");
        	return true;
        case KEY_FORWARD:
        	increaseDirection(1);
        	return true;
        case KEY_REVERSE:
        	increaseDirection(-1);
        	return true;
        }
        return super.handleKeyPress(code, rawcode);
    }
    
    public void increaseDirection(int value) {
    	String mode = shuttleBar.getMode();
    	int index = 3;
    	for (int i=0; i < MODES.length; i++) {
    		if (MODES[i].equals(mode)) {
    			index = i;
    			break;
    		}
    	}
		int newIndex = index + value;
		if (newIndex >= 0 && newIndex < MODES.length) {
        	shuttleBar.setMode(MODES[newIndex]);
            
            int newId = Math.abs(newIndex-3);
            int id = Math.abs(index-3);
            if (newId > id) {
                getBApp().play("speedup" + newId + ".snd");
            } else {
                getBApp().play("slowdown1.snd");
            }
        }
    }
    
    /**
     *  Title of the screen.
     */
    public String toString() 
    {
        return "BShuttleBar";
    }

}
